package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 语音消息
 * 
 * @author iPan
 * @version 2013-12-23
 */
public class VoiceMsg extends PlainMsg {

	/** serialVersionUID */
	private static final long serialVersionUID = 746244402168202445L;

	/** 语音消息媒体id（必须） */
	protected String mediaId;

	/** 语音格式，如amr，speex等 */
	protected String format;

	/** 语音识别结果，UTF8编码 */
	protected String recognition;
	
	public VoiceMsg() {
		super(TransportType.RECEIVE_AND_REPLY);
	}
	
	public VoiceMsg(Document doc) {
		this();
		fromXml(doc);
	}

	public String getMediaId() {
		return mediaId;
	}

	public void setMediaId(String mediaId) {
		this.mediaId = mediaId;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getRecognition() {
		return recognition;
	}

	public void setRecognition(String recognition) {
		this.recognition = recognition;
	}

	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		this.mediaId = root.elementTextTrim(EL_MEDIA_ID);
		this.format = root.elementTextTrim(EL_FORMAT);
		this.recognition = root.elementTextTrim(EL_RECOGNITION);
	}

	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_VOICE).append(wrapElementEnd(EL_MSG_TYPE));
		if (nonEmpty) {
			buf.append(wrapElementHead(EL_VOICE));
			if (StringUtils.isNotEmpty(this.mediaId)) {
				buf.append(wrapElementHead(EL_MEDIA_ID)).append(this.mediaId).append(wrapElementEnd(EL_MEDIA_ID));
			}
			buf.append(wrapElementEnd(EL_VOICE));
		} else {
			buf.append(wrapElementHead(EL_VOICE));
			buf.append(wrapElementHead(EL_MEDIA_ID)).append(getStringValue(this.mediaId)).append(wrapElementEnd(EL_MEDIA_ID));
			buf.append(wrapElementEnd(EL_VOICE));
		}
		return buf.append("</xml>").toString();
	}
}

/*---------------------------------------------
接收：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId>
<Format><![CDATA[Format]]></Format>
<MsgId>1234567890123456</MsgId>
</xml>

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId>
<Format><![CDATA[Format]]></Format>
<Recognition><![CDATA[腾讯微信团队]]></Recognition>
<MsgId>1234567890123456</MsgId>
</xml>

回复：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[media_id]]></MediaId>
</Voice>
</xml>
--------------------------------------------*/